package com.kady.service.impl;

import com.kady.Dao.FoodDao;
import com.kady.contant.BeanFactoryConstant;
import com.kady.entity.Food;
import com.kady.entity.FoodType;
import com.kady.factory.BeanFactory;
import com.kady.service.FoodService;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: kady
 * \* Date: 2021/3/25
 * \* Time: 14:35
 * \* 千峰JAVA2101班
 * \* Description: 菜品业务层
 * \
 */
public class FoodServiceImpl implements FoodService {
    private FoodDao foodDao = (FoodDao) BeanFactory.getBean(BeanFactoryConstant.FOOD_DAO);

    @Override
    public List<Food> getFoodList(String keyword) throws SQLException, InvocationTargetException, IllegalAccessException {
        List<Map<String,Object>> list = foodDao.selectFoodList("%"+(keyword==null?"":keyword.trim())+"%",null,null);
        List<Food> res = new ArrayList<>();
        for(Map<String,Object> item:list){
            Food food = new Food();
            BeanUtils.populate(food,item);
            FoodType foodType = new FoodType((long)food.getTypeId(),(String)item.get("typeName"));
            food.setFoodType(foodType);
            res.add(food);
        }
        return res;
    }

    @Override
    public void addFood(Food food) throws SQLException {
        foodDao.addFood(food.getFoodName(),food.getPrice(),food.getVipPrice(),
                food.getTypeId(),food.getFoodImage(),food.getFoodDesc());
    }

    @Override
    public void updateFood(Food food) throws SQLException {
        foodDao.updateFood(food.getFoodName(),food.getPrice(),food.getVipPrice(),
                food.getTypeId(),food.getFoodImage(),food.getFoodDesc(),food.getFoodId());
    }

    @Override
    public Food findById(String food_id) throws SQLException {
        Food res = foodDao.selectFood(food_id);
        return res;
    }

    @Override
    public  List<Food> findByTypeId(long typeId) throws SQLException {
        List<Food> res = foodDao.selectFoodByTypeId(typeId);
        return res;
    }

    @Override
    public void removeFood(String foodId) throws SQLException {
        foodDao.deleteFood(foodId);
    }

    @Override
    public Food getFoodByName(String name) throws SQLException {
        Food res = foodDao.selectFoodByName(name);
        return res;
    }



}
